﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Base;
using System.ComponentModel;
using Core.Base;
using Components;
using System.Data;

namespace Core.Component
{
    public class SGVComboBoxColumn: DataGridViewComboBoxColumn, IBindingControl, ISGVControl
    {
        BaseModule module;
        BaseEntity entity;

        public SGVComboBoxColumn()
            : base()
        {
            this.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
            this.Frozen = false;
            this.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;

        }

        public Core.Component.UIComponent component
        {
            set;
            get;
        }

        #region Constant
        public const String strBindingColumnTag = "BindingColumn";
        public const String strBindingTableTag = "BindingTable";
        public const String strDataSourceDisplayColumnTag = "DataSourceDisplayColumn";
        public const String strDataSourceKeyColumnTag = "DataSourceKeyColumn";
        public const String strDataSourceTableTag = "DataSourceTableColumn";
        #endregion


        #region IBindingControl
        String _bindingPropertyName = "Text";

        String _bindingTable;
        String _bindingColumn;
        String _dataSourceTable;
        String _dataSourceDisplayColumn;
        String _dataSourceKeyColumn;
        String _dataSourceColumns;

        String _sGVShortcutString = "F2: DUYỆT DANH SÁCH";
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String SGVShortcutString
        {
            get { return _sGVShortcutString; }
            set { _sGVShortcutString = value; }
        }
        String _sGVTag;
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string SGVTag
        {
            get
            {
                return _sGVTag;
            }
            set
            {
                _sGVTag = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        string IBindingControl.BindingPropertyName
        {
            get
            {
                return _bindingPropertyName;
            }
            set
            {
                _bindingPropertyName = value;
                //GeneratePropertyName();
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingTable
        {
            get
            {
                return _bindingTable;
            }
            set
            {
                _bindingTable = value;
                //GeneratePropertyName();
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingColumn
        {
            get
            {
                return _bindingColumn;
            }
            set
            {
                _bindingColumn = value;
                //GeneratePropertyName();
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceTable
        {
            get
            {
                return _dataSourceTable;
            }
            set
            {
                _dataSourceTable = value;
                //GeneratePropertyName();
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceDisplayColumn
        {
            get
            {
                return _dataSourceDisplayColumn;
            }
            set
            {
                _dataSourceDisplayColumn = value;
                //GeneratePropertyName();
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceKeyColumn
        {
            get
            {
                return _dataSourceKeyColumn;
            }
            set
            {
                _dataSourceKeyColumn = value;
               // GeneratePropertyName();
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String DataSourceColumns
        {
            get
            {
                return _dataSourceColumns;
            }
            set
            {
                _dataSourceColumns = value;
                //GeneratePropertyName();
            }
        }

        public void RegisterModule(Core.Base.BaseModule _module)
        {
            if (_module != null)
            {
                this.module = _module;
                this.entity = _module.entity;
            }
        }

        #endregion

        public void GeneratePropertyName()
        {

            String strBindingColumnTag = String.Empty;
            String strBindingTableTag = String.Empty;
            String strDataSourceDiplayColumnTag = String.Empty;
            String strDataSourceKeyColumnTag = String.Empty;
            String strDataSourceTableTag = String.Empty;

            if(String.IsNullOrEmpty(BindingColumn) == false)
                strBindingColumnTag = String.Format("{0}:{1}", "BindingColumn", BindingColumn);
            if (String.IsNullOrEmpty(BindingTable) == false)
                strBindingTableTag = String.Format("{0}:{1}", "BindingTable", BindingTable);
            if (String.IsNullOrEmpty(DataSourceDisplayColumn) == false)
                strDataSourceDiplayColumnTag = String.Format("{0}:{1}", "DataSourceDisplayColumn", DataSourceDisplayColumn);
            if (String.IsNullOrEmpty(DataSourceKeyColumn) == false)
                strDataSourceKeyColumnTag = String.Format("{0}:{1}", "DataSourceKeyColumn", DataSourceKeyColumn);
            if (String.IsNullOrEmpty(DataSourceTable) == false)
                strDataSourceTableTag = String.Format("{0}:{1}", "DataSourceTable", DataSourceTable);

            StringBuilder strTagBuilder = new StringBuilder();

            if(String.IsNullOrEmpty(strBindingColumnTag) == false)
                strTagBuilder.Append(String.Format("{0}|", strBindingColumnTag));
            if (String.IsNullOrEmpty(strBindingTableTag) == false)
                strTagBuilder.Append(String.Format("{0}|", strBindingTableTag));
            if (String.IsNullOrEmpty(strDataSourceDiplayColumnTag) == false)
                strTagBuilder.Append(String.Format("{0}|", strDataSourceDiplayColumnTag));
            if (String.IsNullOrEmpty(strDataSourceKeyColumnTag) == false)
                strTagBuilder.Append(String.Format("{0}|", strDataSourceKeyColumnTag));
            if (String.IsNullOrEmpty(strDataSourceTableTag) == false)
                strTagBuilder.Append(String.Format("{0}|", strDataSourceTableTag));

            DataPropertyName = strTagBuilder.ToString();

        }

        public void DestrucPropertyName()
        {

            if (String.IsNullOrEmpty(DataPropertyName)) return;
            String strColumnTag = DataPropertyName;
            DataPropertyName = String.Empty;

            String[] lstTags = strColumnTag.Split('|');

            if (lstTags.Length >= 1)
                DataPropertyName = lstTags[0];
            if (lstTags.Length >= 2)
                BindingTable = lstTags[1];
            if (lstTags.Length >= 3)
                DataSourceDisplayColumn = lstTags[2];
            if (lstTags.Length >= 4)
                DataSourceKeyColumn = lstTags[3];
            if (lstTags.Length >= 5)
                DataSourceTable = lstTags[4];

        }

        public void Init()
        {
            if (this.ReadOnly == true)
            {
                this.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            }
            DestrucPropertyName();
            InitDataSource();
            this.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
            //this.DefaultCellStyle.Font = new System.Drawing.Font(this.DefaultCellStyle.Font.FontFamily, this.DefaultCellStyle.Font.Size, System.Drawing.FontStyle.Bold);
        }

        public virtual void InitDataSource()
        {
            try
            {
                if (String.IsNullOrEmpty(DataSourceTable)) return;

                DataTable dt = AppContext.GetDataSource(DataSourceTable);
                if (dt.Rows.Count > 0 && Convert.ToInt32(dt.Rows[0]["ID"]) != 0)
                {
                    DataRow drDummy = dt.NewRow();
                    drDummy["ID"] = 0;
                    dt.Rows.InsertAt(drDummy, 0);
                }

                this.DataSource = dt;
                this.ValueMember = DataSourceKeyColumn;
                this.DisplayMember = DataSourceDisplayColumn;
            }
            catch (Exception ex)
            {

            }


            //this.Columns
        }

        public void UpdateFormat()
        {
        }

        public UIComponent Serialize()
        {
            return null;
        }

        public void Deserialize(UIComponent component)
        {
            throw new NotImplementedException();
        }
    }
}
